package ngat.oss;

import ngat.message.OSS.*;
import ngat.util.logging.*;
import ngat.phase2.*; 
import ngat.instrument.*;
import ngat.phase2.nonpersist.*;

import java.util.*;
import java.io.*;

import jyd.dbms.*;
import jyd.storable.*;
import jyd.collection.*;

/**
 *
 * @author S.N.Fraser
 */
public class GET_TAGImpl extends TransactionImpl {

    /** The path used to identify the TAG in the OSS database. */
    protected Path tagPath;

    /** Create a GET_TAGImpl for the specified request.*/
    public GET_TAGImpl(GET_TAG request) {
	super(request);
	this.tagPath = request.getTagPath();
    }

    /**
     * Retrieves the TAG specified.  
     */
    public TRANSACTION_DONE exec() { 

	myLog = LogManager.getLogger("TRACE");

	Tag tag = null;
	
	Transaction t = null;
	try {
	    t = Transaction.current();
	    myLog.log(1, CLASS, id, "exec", 
		      "Fetching TAG: "+tagPath);
	    tag = null;
	    try {
		tag = OSS_Utilities.fetchTag(tagPath);
		myLog.log(1, CLASS, id, "exec", "TAG was fetched ok");
	    } catch (Exception iox) {
		myLog.log(1, CLASS, id, "exec",
			   "TAG: "+tagPath+
			   " Error fetching TAG from Database: "+iox);
		return error(TRANSACTION.DATABASE_READ_ERROR,			   
				     "TAG: "+tagPath+
				     ": Error reading TAG from database: "+iox);
	    }		
	    
	    // Check TAG exists.
	    if (tag == null) {
		// Couldn't find the TAG.
		myLog.log(1, CLASS, id, "exec", 
			   "Tag: "+tagPath+" Not in database.");
		return error(TRANSACTION.PATH_RESOLUTION_ERROR, 
			     "Could not find Tag: "+tagPath);
	    } 

	    // Done
	    GET_TAG_DONE done = new GET_TAG_DONE(id);
	    done.setSuccessful(true);
	    done.setTag(tag);
	    return done;	    

	} finally {
	    t.commit();
	}	
    }
    
} // Class Def. [GET_TAGImpl].



